package com.seeyon.apps.kk.listener.form;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.collaboration.event.CollaborationAppointStepBackEvent;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.kk.form.workflowevent.GFSCLHExtendPlug1;
import com.seeyon.apps.kk.listener.BaseListener;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.ctp.common.po.template.CtpTemplate;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.kk.utils.ValueUtils;

public class InvitationLisener extends BaseListener {
	private static final Log log = LogFactory.getLog(InvitationLisener.class);

	private FreemarkFlowManager freemarkFlowManager;

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	@ListenEvent(event = CollaborationAppointStepBackEvent.class, async = true)
	public void AppointStepBackEvent(CollaborationAppointStepBackEvent event) throws Exception {
		Thread.sleep(BaseListener.LISTENER_THREAD_SLEEPTIME);

		String templetSubject = ValueUtils.getConfig().getString("kk.JetFlowInvitationListener");
		ColSummary summary = this.colManager.getColSummaryById(event.getSummaryId());
		CtpTemplate template = this.templateManager.getCtpTemplate(summary.getTempleteId());
		String[] templetSubjects = StringUtils.split(templetSubject, ",");

		if (ArrayUtils.contains(templetSubjects, template.getTempleteNumber())) {
			Long id = summary.getFormRecordid(); // 表单ID
			String findDatasql = this.freemarkFlowManager.fltTemplateIntoString("invitation/refuseSql/GfsRefuseFndsSql.ftl", null);
			List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(findDatasql, id);
			for (Map<String, Object> map : queryForList) {
				String taxpsn = MapUtils.getString(map, "field0038");
				if (taxpsn.equals(ValueUtils.getConfig().getString("kk.InvitationExit"))) {
					String UpdateZtsql = this.freemarkFlowManager.fltTemplateIntoString("invitation/refuseSql/GfsSpRefuseUpdateSql.ftl", map);
					this.jdbcTemplate.update(UpdateZtsql);
					log.info("KK:" + UpdateZtsql);
				}
			}
		}
	}

}
